package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import easier.log.logger.Logger;
import easier.recycler.DataViewHolder;
import easier.recycler.SingleTypeAdapter;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class MainActivity7 extends AppCompatActivity {

    private RecyclerView mRecycler;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity7.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main7 );
        initView();
        initViewAction();
    }

    private void initView() {

        mRecycler = (RecyclerView) findViewById( R.id.recycler );
    }

    private void initViewAction() {

        mRecycler.setLayoutManager( new LinearLayoutManager( this ) );
        Adapter adapter = new Adapter();
        mRecycler.setAdapter( adapter );

        List< String > result = new ArrayList<>();
        for ( int i = 0; i < 50; i++ ) {
            result.add( "item: " + i );
        }
        adapter.updateSource( result );
    }

    private class Adapter extends SingleTypeAdapter< String > {

        @Override
        protected DataViewHolder< String > buildHolder( int i, String s ) {

            return new DataViewHolder<>( R.layout.recycler_item_00, s ) {

                @Override
                protected void onBind( ViewHolder viewHolder, View root, int position, int type, List< Object > payloads ) {

                    super.onBind( viewHolder, root, position, type, payloads );
                    ((TextView) root.findViewById( R.id.text )).setText( s );
                    Logger.d( "bind:" + viewHolder );

                    Observable.just( s + "-new-" + position )
                        .delay( 5, TimeUnit.SECONDS )
                        .observeOn( AndroidSchedulers.mainThread() )
                        .subscribe( new Observer< String >() {
                            @Override
                            public void onComplete() {

                            }

                            @Override
                            public void onError( Throwable e ) {

                            }

                            @Override
                            public void onSubscribe( @NonNull Disposable d ) {

                            }

                            @Override
                            public void onNext( String s ) {

                                ((TextView) root.findViewById( R.id.text )).setText( s );
                            }
                        } );

                }

                @Override
                protected void onDetachedFromWindow( ViewHolder viewHolder, View root ) {

                    super.onDetachedFromWindow( viewHolder, root );

                    Logger.d( "onDetachedFromWindow:" + viewHolder );
                }
            };
        }
    }
}